package com.example.androidmainshixun.view;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.androidmainshixun.R;
import com.example.androidmainshixun.base.BaseFragment;
import com.example.androidmainshixun.database.DBHelper;
import com.example.androidmainshixun.entity.PersonalInfo;
import com.example.androidmainshixun.entity.User;
import com.example.androidmainshixun.utils.SpUtils;

public class PerCenFrag extends BaseFragment{
    private SpUtils mSpUtils;
    private EditText musername, mnumber, mage;
    private AutoCompleteTextView maddress;
    private Button btn_save;
    private RelativeLayout medit_username,medit_phone,medit_age,medit_adress;
    @Override
    protected int getLayoutId() {
        return R.layout.personal_center_fragment;
    }
    @Override
    @SuppressLint("Range")
    protected void initView() {
        musername = mView.findViewById(R.id.username);
        mnumber = mView.findViewById(R.id.number);
        mage = mView.findViewById(R.id.age);
        maddress = mView.findViewById(R.id.prr_address);
        btn_save=mView.findViewById(R.id.btn_save);

        medit_username=mView.findViewById(R.id.edit_username);
        medit_phone=mView.findViewById(R.id.edit_phone);
        medit_age=mView.findViewById(R.id.edit_age);
        medit_adress=mView.findViewById(R.id.edit_adress);
        
        mSpUtils = new SpUtils(mContext);
        //检测登录状态
        int loginId = mSpUtils.getInt(SpUtils.LOGIN_KEY, -1);
        if (loginId >= 0) {
            // 执行关联查询
            PersonalInfo personalInfo = DBHelper.getInstance(mContext).getPersonalInfoByUserId(loginId);
            // 假设getUserPersonalInfo方法接受用户ID作为参数
            /*有相应用户信息，则显示。点击底部按钮，则执行修改*/
            if (personalInfo != null) {
                // 将获取到的数据设置到界面上
                musername.setText(personalInfo.getUsername());
                mnumber.setText(personalInfo.getNumber());
                mage.setText(String.valueOf(personalInfo.getAge()));
                maddress.setText(personalInfo.getAddress());
                // 假设您已经有一个按钮的点击事件处理方法
                btn_save.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 从界面获取用户输入的新数据
                        String newUsername = musername.getText().toString();
                        String newNumber = mnumber.getText().toString();
                        int newAge = Integer.parseInt(mage.getText().toString());
                        String newAddress = maddress.getText().toString();
                        // 创建或更新 PersonalInfo 对象
                        PersonalInfo newPersonalInfo = new PersonalInfo();
                        newPersonalInfo.setUsername(newUsername);
                        newPersonalInfo.setNumber(newNumber);
                        newPersonalInfo.setAge(newAge);
                        newPersonalInfo.setAddress(newAddress);
                        // 调用 updatePersonalInfo 方法来更新数据库
                        SQLiteDatabase db = DBHelper.getInstance(mContext).getWritableDatabase(); // 或 getReadableDatabase()
                        User user = DBHelper.getInstance(mContext).getUserById(loginId);
                        String userid= String.valueOf(user.getUserid());
                        newPersonalInfo.setUserid(userid);
                        int rowsAffected = DBHelper.getInstance(mContext).updatePersonalInfo(db, newPersonalInfo,userid);
                        if (rowsAffected > 0) {
                            Toast.makeText(getContext(), "修改成功", Toast.LENGTH_SHORT).show();
                            musername.setText("");
                            mnumber.setText("");
                            mage.setText("");
                            maddress.setText("");
                        }
                        }
                });
            }/*没有用户信息，点击底部按钮，则允许添加*/
            else if(personalInfo == null) {
                // 如果没有查询到数据，可以给出提示
                Toast.makeText(mContext, "未查询到用户信息", Toast.LENGTH_SHORT).show();
                btn_save.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String username = musername.getText().toString();
                        String number = mnumber.getText().toString();
                        String age = mage.getText().toString();
                        String address = maddress.getText().toString();

                        if (allInputNotEmpty(username,number,age,address)) {
                            PersonalInfo personalInfo = new PersonalInfo();
                            personalInfo.setUsername(username);
                            personalInfo.setNumber(number);
                            personalInfo.setAge(Integer.parseInt(age));
                            personalInfo.setAddress(address);
                            User user = DBHelper.getInstance(mContext).getUserById(loginId);
                            String userid= String.valueOf(user.getUserid());
                            personalInfo.setUserid(userid);
                            int id1 = (int) DBHelper.getInstance(getContext()).insertpersonalInfo5(personalInfo,userid);
                            if (id1 > 0) {
                                Toast.makeText(getContext(), "保存成功", Toast.LENGTH_SHORT).show();
                                Intent intent=new Intent(getContext(),PersonalCenter.class);
                                startActivity(intent);
                            }
                        }else {
                            Toast.makeText(getContext(), "信息不完善", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }
        else {
            // 如果没有登录用户，可以给出提示或跳转
            Toast.makeText(mContext, "暂无用户信息，请登录", Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    protected void initListener() {

    }
    private boolean allInputNotEmpty(String... inputStrings) {
        for (int i = 0; i < inputStrings.length; i++) {
            if (TextUtils.isEmpty(inputStrings[i])) {
                return false;
            }
        }
        return true;
    }
}
